-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: supports optional dependencies #168
Conversation
|
Name | Link |
---|---|
🔨 Latest commit | 20c4eae |
const optionalDependencies = { | ||
minifyCss: ['cssnano', 'postcss'], | ||
minifyJs: ['terser'], | ||
minifyUrl: ['relateurl', 'srcset', 'terser'], | ||
minifySvg: ['svgo'], | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
purgecss
and uncss
are not included here since only one of them is required when enabled.
(optionalDependencies[moduleName] || []).forEach(dependency => { | ||
try { | ||
require(dependency); | ||
} catch (e) { | ||
if (e.code === 'MODULE_NOT_FOUND') { | ||
console.warn(`You have to install "${dependency}" in order to use htmlnano's "${moduleName}" module`); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to resolve the required dependencies before HTML actually being minified. With Node.js's require cache it won't add any performance overhead when those dependencies are required again.
htmlnano.getRequiredOptionalDependencies = function (optionsRun, presetRun) { | ||
const [options] = loadConfig(optionsRun, presetRun); | ||
|
||
return [...new Set(Object.keys(options).filter(moduleName => options[moduleName]).map(moduleName => optionalDependencies[moduleName]).flat())]; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getRequiredOptionalDependencies
API is designed for other libraries that utilize htmlnano.
For example, parcel-bundler has a plugin @parcel/optimizer-htmlnano
built on top of htmlnano
. This API should enable the parcel to install those optional dependencies automatically.
Ideas and opinions from parcel developers (@devongovett @mischnic) would be appreciated.
Sure. Good idea! I created it and going to merge your PR to |
Fix #165, close #166.
Make
terser
,cssnano
,uncss
, etc. optional.BREAKING CHANGES
Only merge it when it is about to bump a major version.
@maltsev What about creating a new branch
v2
? I'd like to introduce other breaking changes on top of this PR (like makingpurgecss
default instead ofuncss
). With a new branch, we can only merge breaking changes intomaster
when2.0.0
is about to release.